package com.hyyt.shopjsp.wxmg.material.controller;

import com.hyyt.shopjsp.basic.pojo.BasicUsers;
import com.hyyt.shopjsp.basic.service.IBasicUsersService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.StringStaticKey;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import com.hyyt.shopjsp.util.redis.util.RedisDataReadUtil;
import com.hyyt.shopjsp.wxmg.fans.pojo.WxFansGroup;
import com.hyyt.shopjsp.wxmg.fans.pojo.WxFansUserInfo;
import com.hyyt.shopjsp.wxmg.fans.service.IWxFansGroupService;
import com.hyyt.shopjsp.wxmg.fans.service.IWxFansUserInfoService;
import com.hyyt.shopjsp.wxmg.material.pojo.WxMaterialImageTxtInfo;
import com.hyyt.shopjsp.wxmg.material.service.IWxMaterialImageTxtInfoService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * WX_图文素材信息 Controller
 *
 * LC
 */
@Controller
@RequestMapping("/back/wx/material/imageTxt")
public class WxMaterialImageTxtInfoController extends BaseController {

    private Logger logger = Logger.getLogger(WxMaterialImageTxtInfoController.class);
    private String basePath = "/back/wxmg/material/imageTxtInfo/";

    @Resource
    private IWxMaterialImageTxtInfoService wxMaterialImageTxtInfoService;
    @Resource
    private IBasicUsersService usersService;
    @Resource
    private IWxFansUserInfoService wxFansUserInfoService;
    @Resource
    private IWxFansGroupService wxFansGroupService;

    /**
     * 进入图文素材信息页面
     *
     * @return 返回到指定页面
     */
    @RequestMapping("gotoMaterialImageTxtPage")
    public String gotoMaterialImagePage (){
        return basePath + "materialImageTxtInfoList";
    }

    /**
     * 查询所有图文素材信息列表
     * @param page
     * @param materialTitle             素材图文标题
     * @param lastUpdateDateStart       最后修改开始时间
     * @param lastUpdateDateEnd         最后修改结束时间
     * @param deletedFlag               删除标记：0 正常、1 删除
     */
    @RequestMapping(value = "listMaterialImageTxt",method = RequestMethod.POST)
    @ResponseBody
    public void listMaterialImageTxt(String materialTitle,String lastUpdateDateStart,String lastUpdateDateEnd,String deletedFlag, Page page, HttpServletResponse response){
        Map<String,Object> param = new HashMap<>();
        String publicNoInfoStr = RedisDataReadUtil.getRedisValue(StringStaticKey.WX_PUBLIC_NO_INFO);
        Map<String,Object> publicNoInfo = FastJsonUtils.stringToCollect(publicNoInfoStr);
        if(Utils.objectIsNotEmpty(publicNoInfo) && Utils.objectIsNotEmpty(publicNoInfo.get("publicNumberId"))){
            String publicNumberId = String.valueOf(publicNoInfo.get("publicNumberId"));
            param.put("publicNumberId",publicNumberId);
        }
        if (Utils.objectIsNotEmpty(materialTitle)) {
            param.put("materialTitle", materialTitle);
        }
        if(Utils.objectIsNotEmpty(lastUpdateDateStart)){
            param.put("lastUpdateDateStart",lastUpdateDateStart);
        }
        if(Utils.objectIsNotEmpty(lastUpdateDateEnd)){
            param.put("lastUpdateDateEnd",lastUpdateDateEnd);
        }
        if(Utils.objectIsNotEmpty(deletedFlag)){
            param.put("deletedFlag",deletedFlag);
        }
        param.put("sortColumns"," lastUpdateDate desc");
        int totalRecordCount = wxMaterialImageTxtInfoService.countWxMaterialImageTxtInfo(param);
        page.setPageInfo(page.getPageSize(),totalRecordCount,page.getCurrentPage());
        List<WxMaterialImageTxtInfo> materialImageTxtInfoList = wxMaterialImageTxtInfoService.queryWxMaterialImageTxtInfo(param,page.getPageRecordBeginIndex(),page.getPageSize());
        param.clear();
        param.put("total",totalRecordCount);
        param.put("rows",materialImageTxtInfoList);
        ResultMsg.create(param,response);
    }

    /**
     * 根据id获得图文素材信息对象
     * @param materialImageTxtInfoId
     * @return
     */
    @RequestMapping(value = "getWxMaterialImageTxtInfoObject",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getWxMaterialImageTxtInfoObject(Integer materialImageTxtInfoId){
        if (Utils.objectIsNotEmpty(materialImageTxtInfoId)){
            Map<String,Object> params = new HashMap<>();
            WxMaterialImageTxtInfo wxMaterialImageTxtInfo = wxMaterialImageTxtInfoService.loadWxMaterialImageTxtInfo(materialImageTxtInfoId);
            BasicUsers creater = usersService.loadBasicUsers(wxMaterialImageTxtInfo.getCreater());
            if(Utils.objectIsNotEmpty(creater) && Utils.objectIsNotEmpty(creater.getUserName())){
                params.put("createrName",creater.getUserName());
            }
            BasicUsers modifier = usersService.loadBasicUsers(wxMaterialImageTxtInfo.getModifier());
            if(Utils.objectIsNotEmpty(modifier) && Utils.objectIsNotEmpty(modifier.getUserName())){
                params.put("modifierName",modifier.getUserName());
            }
            params.put("wxMaterialImageTxtInfo",wxMaterialImageTxtInfo);
            return ResultMsg.create(true,null, FastJsonUtils.toJSONString(params));
        }
        return ResultMsg.create(false);
    }

    /**
     * 根据id删除图文素材信息（更改删除标记为 1：删除）
     * @param materialImageTxtInfoId
     * @return
     */
    @RequestMapping(value = "deleteWxMaterialImageTxtInfo",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteWxMaterialImageTxtInfo(Integer materialImageTxtInfoId){
        if (Utils.objectIsNotEmpty(materialImageTxtInfoId)){
            WxMaterialImageTxtInfo wxMaterialImageTxtInfo = wxMaterialImageTxtInfoService.loadWxMaterialImageTxtInfo(materialImageTxtInfoId);
            BasicUsers basicUsers = getSessionUsers();
            if(Utils.objectIsNotEmpty(wxMaterialImageTxtInfo)){
                wxMaterialImageTxtInfo.setModifier(basicUsers.getUsersId());
                wxMaterialImageTxtInfo.setLastUpdateDate(new Timestamp(System.currentTimeMillis()));
                wxMaterialImageTxtInfo.setDeletedBy(basicUsers.getUsersId());
                wxMaterialImageTxtInfo.setDeletedFlag(1);
                wxMaterialImageTxtInfo.setDeletionDate(new Timestamp(System.currentTimeMillis()));
                wxMaterialImageTxtInfoService.updateWxMaterialImageTxtInfo(wxMaterialImageTxtInfo);
                return ResultMsg.create(true);
            }
        }else {
            return ResultMsg.create(false);
        }
        return ResultMsg.create(false);
    }

    /**
     * 保存或者修改图文素材信息
     * @param wxMaterialImageTxtInfo
     * @return
     */
    @RequestMapping(value = "saveOrUpdateWxMaterialImageTxtInfo",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveOrUpdateWxMaterialImageTxtInfo(WxMaterialImageTxtInfo wxMaterialImageTxtInfo){
        if (Utils.objectIsNotEmpty(wxMaterialImageTxtInfo)){
            BasicUsers basicUsers = getSessionUsers();
            if (Utils.objectIsEmpty(wxMaterialImageTxtInfo.getMaterialImageTxtInfoId())){
                wxMaterialImageTxtInfo.setCreater(basicUsers.getUsersId());
                wxMaterialImageTxtInfo.setCreateTime(new Timestamp(System.currentTimeMillis()));
                wxMaterialImageTxtInfo.setModifier(basicUsers.getUsersId());
                wxMaterialImageTxtInfo.setLastUpdateDate(new Timestamp(System.currentTimeMillis()));
                wxMaterialImageTxtInfoService.saveWxMaterialImageTxtInfo(wxMaterialImageTxtInfo);
            }else{
                wxMaterialImageTxtInfo.setModifier(basicUsers.getUsersId());
                wxMaterialImageTxtInfo.setLastUpdateDate(new Timestamp(System.currentTimeMillis()));
                wxMaterialImageTxtInfoService.updateWxMaterialImageTxtInfo(wxMaterialImageTxtInfo);
            }
        }
        return ResultMsg.create(true);
    }

    /**
     * 进入图文素材回收站页面
     * @return
     */
    @RequestMapping("gotoMaterialImageTxtRecycleBinPage")
    public String gotoMaterialImageTxtRecycleBinPage (){
        return basePath + "materialImageTxtRecycleBinList";
    }

    /**
     * 根据id删除图文素材信息（永久删除信息：删除记录）
     * @param materialImageTxtInfoId
     * @return
     */
    @RequestMapping(value = "deleteMaterialImageTxtInfoForever",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteMaterialImageTxtInfoForever(Integer materialImageTxtInfoId){
        if (Utils.objectIsNotEmpty(materialImageTxtInfoId)){
            wxMaterialImageTxtInfoService.deleteWxMaterialImageTxtInfo(materialImageTxtInfoId);
            return ResultMsg.create(true);
        }else {
            return ResultMsg.create(false);
        }
    }

    /**
     * 根据id还原图文素材信息
     * @param materialImageTxtInfoId
     * @return
     */
    @RequestMapping(value = "restoreMaterialImageTxtInfo",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg restoreMaterialImageTxtInfo(Integer materialImageTxtInfoId){
        if (Utils.objectIsNotEmpty(materialImageTxtInfoId)){
            wxMaterialImageTxtInfoService.restoreWxMaterialImageTxtInfo(materialImageTxtInfoId);
            return ResultMsg.create(true);
        }else {
            return ResultMsg.create(false);
        }
    }

    /**
     * 查询所有粉丝用户信息列表
     *
     * @param nickName 会员昵称
     * @param userOpenId 会员标识OPENID
     * @param page 分页
     * @param response 返回结果集
     */
    @RequestMapping(value = "listFansUserInfoInMaterialImageTxt",method = RequestMethod.POST)
    @ResponseBody
    public void listFansUserInfoInMaterialImageTxt(String nickName, String userOpenId, Page page, HttpServletResponse response){
        Map<String,Object> param = new HashMap<>();
        if (Utils.objectIsNotEmpty(nickName)) {
            param.put("nickName", nickName.trim());
        }
        if (Utils.objectIsNotEmpty(userOpenId)) {
            param.put("userOpenId", userOpenId.trim());
        }
        int totalRecordCount = wxFansUserInfoService.countWxFansUserInfo(param);
        param.put("sortColumns","userId desc");
        page.setPageInfo(page.getPageSize(),totalRecordCount,page.getCurrentPage());
        List<WxFansUserInfo> wxFansUserInfo = wxFansUserInfoService.queryWxFansUserInfo(param,page.getPageRecordBeginIndex(),page.getPageSize());
        param.clear();
        param.put("total",totalRecordCount);
        param.put("rows",wxFansUserInfo);
        ResultMsg.create(param,response);
    }

    /**
     *查询所有粉丝分组列表
     *
     * @param name 组名称
     * @param page 分页
     * @param response 返回结果集
     */
    @RequestMapping(value = "listFansGroupInMaterialImageTxt",method = RequestMethod.POST)
    @ResponseBody
    public void listFansGroupInMaterialImageTxt(String name, Page page, HttpServletResponse response){
        Map<String,Object> param = new HashMap<>();
        if (Utils.objectIsNotEmpty(name)) {
            param.put("name", name.trim());
        }
        int totalRecordCount = wxFansGroupService.countWxFansGroup(param);
        param.put("sortColumns","fansGroupId desc");
        page.setPageInfo(page.getPageSize(),totalRecordCount,page.getCurrentPage());
        List<WxFansGroup> wxFansGroup = wxFansGroupService.queryWxFansGroup(param,page.getPageRecordBeginIndex(),page.getPageSize());
        param.clear();
        param.put("total",totalRecordCount);
        param.put("rows",wxFansGroup);
        ResultMsg.create(param,response);
    }

}
